package com.cmict.data.infra.dal.mysql.rdb;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cmict.data.framework.mybatis.core.mapper.BaseMapperX;
import com.cmict.data.infra.dal.dataobject.message.MessageTemplateDO;
import com.cmict.data.infra.dal.dataobject.rdb.RdbDO;
import org.apache.ibatis.annotations.Mapper;
import com.cmict.data.infra.controller.admin.rdb.vo.*;
import org.apache.ibatis.annotations.Select;

/**
 * 数据库 Mapper
 *
 * @author awesome1114
 */
@Mapper
public interface RdbMapper extends BaseMapperX<RdbDO> {

    default PageResult<RdbDO> selectPage(RdbPageReqVO reqVO) {
         QueryWrapper<RdbDO> queryWrapper=new QueryWrapper<>();
        if(reqVO.getKeyword()!=null){
            queryWrapper.lambda().and(qw -> qw.like(RdbDO::getDatabaseName, reqVO.getKeyword())
                    .or().like(RdbDO::getAlias, reqVO.getKeyword()));
        }
        queryWrapper.lambda().orderByDesc(RdbDO::getCreateTime);
        return selectPage(reqVO,queryWrapper);
    }

    default List<RdbDO> selectList(RdbExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<RdbDO>()
                .eqIfPresent(RdbDO::getType, reqVO.getType())
                .eqIfPresent(RdbDO::getIp, reqVO.getIp())
                .eqIfPresent(RdbDO::getPort, reqVO.getPort())
                .likeIfPresent(RdbDO::getUsername, reqVO.getUsername())
                .eqIfPresent(RdbDO::getPassword, reqVO.getPassword())
                .likeIfPresent(RdbDO::getDatabaseName, reqVO.getDatabaseName())
                .eqIfPresent(RdbDO::getAlias, reqVO.getAlias())
                .eqIfPresent(RdbDO::getDescription, reqVO.getDescription())
                .betweenIfPresent(RdbDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(RdbDO::getId));
    }
    @Select("SELECT ip FROM `sys_rdb` WHERE deleted = 0 GROUP BY  ip ")
    Set<RdbRespVO> getIpGroupChildren();
}
